package com.zthz.douxing.sendmessageservice.thirdmessagesender

import com.google.common.eventbus.Subscribe
import com.tencent.xinge.ClickAction
import com.tencent.xinge.Message
import com.tencent.xinge.MessageIOS
import com.tencent.xinge.Style
import com.tencent.xinge.XingeApp
import com.zthz.douxing.event.XinGeMessageEvent
import com.zthz.douxing.exception.PlatformNotFoundException
import com.zthz.douxing.msg.MessageType
import com.zthz.douxing.system.PlatformType

import javax.annotation.PostConstruct

/**
 * 使用信鸽来发送信息给用户
 */
class MessageSenderService {

    def grailsApplication
    def eventBusService

    def xingeApp = [:]

    @PostConstruct
    def init(){
        eventBusService.register(this)
    }

    def getMessageTypeFromDouXing(type){
        if(type == MessageType.RegistCode ||
           type == MessageType.TrainTakeOffAlert ||
           type == MessageType.WeathMsg){
            return Message.TYPE_MESSAGE
        } else {
            return Message.TYPE_NOTIFICATION
        }

    }
    @Subscribe
    def sendXinGeMessage(XinGeMessageEvent event){
        if(event.validate()){
            //如果验证没有问题，将通过飞鸽发送消息到用户手机上
            def msg = null;
            if(event.platformType == PlatformType.android){
                msg = new Message()
                msg.type = getMessageTypeFromDouXing(event.msgType)
                msg.setTitle(event.title)
                msg.setContent(event.content)
            } else if( event.platformType == PlatformType.ios){
                msg = new MessageIOS()
            }

            sendMessageToOne(event.platformType.name(),event.xgToken, msg)
        } else {
            //做错误处理
        }
    }

    def getXingApp(platform) {
        if(xingeApp[platform] == null){
            def config = getXingAppConfig(platform)
            if(config.accessId != null && config.secretKey != null){
                def platformXingeApp = new XingeApp(config.accessId, config.secretKey)
                xingeApp[platform] = platformXingeApp
                return platformXingeApp
            } else {
                log.error("can not found platform: ${platform}")
                throw new PlatformNotFoundException("can not found platform: ${platform}")
            }
        } else  {
            return xingeApp[platform]
        }

    }

    def getXingAppConfig(platform){
        if(platform == "0"){
            return grailsApplication.config.xingge.ios
        } else if(platform == "1"){
            return grailsApplication.config.xingge.android
        } else {
            //window phone
            return [:]
        }
    }

    def sendMessageToOne(platform, userToken, msg) {
        def xgApp = getXingApp(platform)
        //msg.type = Message.TYPE_MESSAGE
        return xgApp.pushSingleDevice(userToken, msg)
    }

    def sendToAllDevice(msg){
        Message xinggeMessage = new Message()
        xinggeMessage.title = msg.m_title
        xinggeMessage.content = msg.m_content
        xinggeMessage.type = Message.TYPE_NOTIFICATION

        def style = new Style(1);
        ClickAction action = new ClickAction();

        action.setActionType(ClickAction.TYPE_INTENT);
        action.setIntent("intent:10086#Intent;scheme=tel;action=android.intent.action.DIAL;S.key=value;end");
        xinggeMessage.setStyle(style);
        xinggeMessage.setAction(action);

        return getXingApp("1").pushAllDevice(XingeApp.DEVICE_ALL, xinggeMessage)
    }

    @Override
    public String toString() {
        return "MessageSenderService{" +
                "grailsApplication=" + grailsApplication +
                ", xingeApp=" + xingeApp +
                '}';
    }
}
